package com.sitech.openapi.controller;

import com.sitech.openapi.componet.SetHttpHeaderCmpt;
import com.sitech.openapi.service.BroadBandProdInstService;
import com.sitech.openapi.service.QryWorkOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.NativeWebRequest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by zhangsq on 2018/12/20.
 */
@RestController
@Slf4j
@Api(tags = "宽带续约", description = "电子渠道宽带续约")
public class BroadBandProdInstController {
    @Autowired
    private SetHttpHeaderCmpt setHttpHeaderCmpt;
    @Autowired
    BroadBandProdInstService broadBandProdInstService;

    @GetMapping("/broadBandProdInst")
    @ApiOperation(value = "查询宽带产品实例信息", notes = "电子渠道宽带续约,查询宽带产品实例信息")
    public ResponseEntity<Map> qryBroadBandProdInst(@RequestParam Map inParam,
                                                 @RequestHeader HttpHeaders headers) throws Exception {
        log.info("查询宽带产品实例信息入参<><><><>inParam=" + inParam);
        String commonRegionId = (String) inParam.get("commonRegionId");
        String certType = (String) inParam.get("certType");
        String certNum = (String) inParam.get("certNum");
        String account = (String) inParam.get("account");
        String lanId = "";
        if(!StringUtils.isEmpty(commonRegionId)) {
             lanId = commonRegionId.substring(1, 5);
        }
        Map inMap = new HashMap();
        inMap.put("certType", certType);
        inMap.put("certNum", certNum);
        inMap.put("lanId", lanId);
        inMap.put("accNbr", account);
        Map retMap = broadBandProdInstService.doQryBroadBandProdInst(inMap);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<Map> res = new ResponseEntity<Map>(retMap, httpHeaders, HttpStatus.OK);
        return res;
    }

    @PostMapping("/broadBandProdOrder")
    @ApiOperation(value = "新增宽带产品订单", notes = "电子渠道宽带续约,新增宽带产品订单")
    public ResponseEntity<Map> qryBroadBandProdOrder(@RequestBody Map inParam,
                                                    @RequestHeader HttpHeaders headers) throws Exception {
        log.info("新增宽带产品订单入参<><><><>inParam=" + inParam);
        Map retMap = broadBandProdInstService.doAddBroadBandProdInst(inParam);
        HttpHeaders httpHeaders = setHttpHeaderCmpt.setOKHttpHeaders(headers);
        ResponseEntity<Map> res = new ResponseEntity<Map>(retMap, httpHeaders, HttpStatus.OK);
        return res;
    }


    @ExceptionHandler
    public ResponseEntity<Map<String, String>> broadBandError(Exception e, NativeWebRequest request) {
        Map<String, String> errors = new HashMap<>();
        errors.put("code", HttpStatus.BAD_REQUEST.toString());
        errors.put("reason", e.getMessage());
        errors.put("message", e.toString());
        errors.put("referenceError", "");

        HttpHeaders httpHeaders = setHttpHeaderCmpt.setErrorHttpHeaders(request);
        ResponseEntity<Map<String, String>> re = new ResponseEntity<>(errors, httpHeaders, HttpStatus.BAD_REQUEST);
        return re;
    }
}
